<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <link href="../css/themes/default/easyui.css" rel="stylesheet" />
    <link href="../css/themes/icon.css" rel="stylesheet" />
    <link href="../css/themes/color.css" rel="stylesheet" />
    <link href="../css/replace_apply.css" rel="stylesheet" />
    <script src="../script/jquery.min.js"></script>
	<script src="../script/easyui-lang-zh_CN.js" ></script>
    <script src="../script/jquery.easyui.min.js"></script>
	<script src="../script/commom.js"></script>
	<script src="../script/checkUser.js"></script>
    <title>添加用户</title>
    <style>
    </style>
</head>
<body>
    <div class="server">
        <h3 class="replace_title" align="center">添加用户</h3>
        <table class="table" align="center">
            <tr>
                <td colspan="6" class="head">用户信息</td>
            </tr>
            <tr>
                <td class="first">使用人ID<span class="xing">*</span>：</td>
                <td class="input"><input type="text" id="id" name="id" onchange="checkID()" /></td>
                <td class="first" id="idMsg"></td>
                <td class="first">用户姓名<span class="xing">*</span>：</td>
                <td class="input"><input type="text" id="name" name="name" onchange="checkName()"/></td>
                <td class="first" style="color: red;" id="nameMsg"></td>
            </tr>
            <tr>
                <td class="first">性别<span class="xing">*</span>：</td>
                <td class="input"><select calss="easyui-combobox" id="sex" name="sex">
					<option value="男">男</option>
					<option value ="女">女</option>
				</select></td>
				<td class="first" id="sexMsg"></td>
                <td class="first">手机号码<span class="xing">*</span>：</td>
                <td class="input"><input type="text" id="phoneNumber" name="phoneNumber" onchange="checkPhone()"/></td>
				<td class="first" id="phoneMsg" style="color: red;"></td>
            </tr>
			<tr>
			    <td class="first">生日<span class="xing">*</span>：</td>
			    <td class="input"><input id="birth" class="easyui-datebox" data-options="formatter:myformatter,parser:myparser" onchange="checkBirth()"></td>
				<td class="first" id="birthMsg" style="color: red;"></td>
			    <td class="first">身份证号码<span class="xing">*</span>：</td>
			    <td class="input"><input id="idCard" type="text" name="idCard" onchange="checkIdCard()"/></td>
				<td class="first" id="idCardMsg" style="color: red;"></td>
			</tr>
            <tr>
                <td class="first">职位<span class="xing">*</span>：</td>
                <td class="input"><select calss="easyui-combobox" id="type" name="type">
					<option value="1">学生</option>
					<option value="2">老师</option>
					<option value="3">专家</option>
					<option value="4">主管</option>
					<option value="5">实验室管理员</option>
					<option value="6">超级管理员</option>
					<option value="7">校外人员</option>
				</select></td>
				<td class="first"  style="color: red;"></td>
                <td class="first">现在住址<span class="xing">*</span>：</td>
                <td class="input"><input class="addr" type="text" id="address" onchange="checkAddress()"/></td>
				<td class="first" id="addressMsg" style="color: red;"></td>

            </tr>
			<tr>
			    <td class="first">标签：</td>
			    <td class="input"><input id="tag" type="text"></td>
				<td class="first" id="birthMsg"></td>
			    <td class="first">邮箱：</td>
			    <td class="input"><input id="email" type="text" name="email" onchange="checkEmail()"/></td>
				<td class="first" id="emailMsg" style="color: red;"></td>
			</tr>
            <tr>
                <td colspan="6" class="head">其他说明</td>
            </tr>
            <tr>
                <td class="secend" style="height:60px;" colspan="6">
                    <textarea id="otherInfo" rows="4" cols="131" style="resize:none;"></textarea>
                </td>
            </tr>
            <tr>
                <td colspan="6" style="height:40px;">
                    <a href="javascript:addUser();" class="easyui-linkbutton c1" style="width:50px;">提交</a>
                </td>
            </tr>
        </table>
    </div>
    <script>
		var idIsOk=false;
		function checkID()
		{
			function check(id){
				var reg=/^[0-9]*$/;
				return !reg.test(id);
			}
			var id=$("#id").val();
			if(id=="")
			{
				idIsOk=false;
				$("#idMsg").css("color","red");
				$("#idMsg").html("×id不能为空")
			}else if(check(id)){
				idIsOk=false;
				$("#idMsg").css("color","red");
				$("#idMsg").html("×id格式为数字");
			}
			else{
				var par={id:id};
				$.ajax({
					type:"post",
					contentType :'application/x-www-form-urlencoded;charset=utf-8',
					dataType:"json",  //数据格式设置为jsonp
					url : 'http://localhost:8080/libManager/user/findById.do',
					data:par, //额外的参数
					success:function (data) 
					{
						if(data.status==1)
						{
							idIsOk=true;
							$("#idMsg").css("color","green");
							$("#idMsg").html("√id可用");
						}else
						{
							idIsOk=false;
							$("#idMsg").css("color","red");
							$("#idMsg").html("×id已拥有");
						}
					}
				});
			}
		}
		function checkAll()
		{
			if(!idIsOk)
			{
				checkID();
				if(!idIsOk)
				{
					return false;
				}
			}
			if(!checkName())
			{
				return false;
			}
			if(!checkPhone())
			{
				return false;
			}
			if(!checkBirth())
			{
				return false;
			}
			if(!checkIdCard())
			{
				return false;
			}
			if(!checkAddress())
			{
				return false;
			}
			if(!checkEmail())
			{
				return false;
			}
			return true;
		}
		function addUser()
		{
			if(checkAll())
			{
				var pars=
				{
					id:$("#id").val(),
					name:$("#name").val(),
					sex:$("#sex").val(),
					phoneNumber:$("#phoneNumber").val(),
					birth:$("#birth").datebox("getValue"),
					idCard:$("#idCard").val(),
					type:$("#type").val(),
					address:$("#address").val(),
					tag:$("#tag").val(),
					email:$("#email").val(),
					otherInfo:$("#otherInfo").val()
				}
				$.ajax({
					type:"post",
					contentType :'application/json;charset=utf-8',
					dataType:"json",  //数据格式设置为jsonp
					url : 'http://localhost:8080/libManager/user/add.do',
					data:JSON.stringify(pars), //额外的参数
					success:function (data) 
					{
						if(data.status==1)
						{
							$("#id").val("");
							$("#name").val("");
							$("#phoneNumber").val("");
							$("#birth").datebox('setValue', '');
							$("#idCard").val("");
							$("#address").val("");
							$("#tag").val("");
							$("#email").val("");
							$("#otherInfo").val("");
						}else
						{
							
						}
						$.messager.alert('系统提示', data.msg);
					}
				});
			}else
			{
				$.messager.alert("系统提醒","部分信息没有完善或格式不对！！");
			}
		}
    </script>
</body>
</html>